package cc.yihy.spider.core;

import cc.yihy.spider.entity.Post;
import cc.yihy.spider.repositories.PostRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Package: cc.yihy
 * @date: 2017/11/21 16:14
 * @author: Yihy
 * @version: V1.0
 * @Description:
 */
@Component
public class JianshuDBPipeline implements Pipeline {


    @Autowired
    private PostRepository postRepository;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");

    public JianshuDBPipeline() {

    }

    @Override
    public void process(ResultItems resultItems, Task task) {

        if (StringUtils.isBlank((String)resultItems.get("title")) || StringUtils.isBlank((String)resultItems.get("content"))) {
            return;
        }

//        2016.09.10 20:00*
        String publishTime = resultItems.get("publishTime");
        if (publishTime == null) return;
        if (publishTime.indexOf("*") != -1) {
            publishTime = publishTime.replace("*", "");
        }


        Date parse = null;
        try {
            parse = simpleDateFormat.parse(publishTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Post post = new Post();

        post.setId((Integer) resultItems.get("id"));
        post.setTitle((String)resultItems.get("title"));
        post.setAuthor((String)resultItems.get("author"));
        post.setTag((String)resultItems.get("tag"));
        post.setPublishTime(parse);
        post.setJsonInfo((String)resultItems.get("jsonInfo"));
        post.setContent(removeEmoji((String)resultItems.get("content")));
        post.setMarkdown(removeEmoji((String)resultItems.get("markdown")));
        post.setUrl(resultItems.getRequest().getUrl());
        try {
            postRepository.save(post);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    private String removeEmoji(String title) {
        if (title != null) {
            title = title.replaceAll("[^\\u0000-\\uFFFF]", "");

        }
        return title;
    }


}
